//
// Bonus5Part.cpp for nibbler in /home/gomes_m//workspace/cpp/nibbler/googleCode
// 
// Made by damien lefebvre
// Login   <lefebv_w@epitech.net>
// 
// Started on  Thu Mar 21 14:23:08 2013 damien lefebvre
// Last update Fri Jul 26 17:27:43 2013 mickael gomes
//

#include	"Bonus5Part.hh"

Bonus5Part::Bonus5Part(void)
  : m_type(IGraphicLibrary::BONUS), m_pos_x(0), m_pos_y(0), m_img(IGraphicLibrary::IMG_BONUS5PART_1), m_lastmove(0), m_timer(0), m_timeToCatch(150), m_framerate(5)
{}

void		Bonus5Part::generatePosition(t_frame frame, std::vector< std::vector<Case *> > const & map, Snake const & snake)
{
  unsigned int	new_pos_x;
  unsigned int	new_pos_y;

  m_timer = frame + random() % 1000;
  do
    {
      new_pos_x = random() % map.size();
      new_pos_y = random() % map[new_pos_x].size();
    } while (map[new_pos_x][new_pos_y]->getType() != IGraphicLibrary::EMPTY ||
	     snake.checkIntersection(new_pos_x, new_pos_y) == true);
  m_pos_x = new_pos_x;
  m_pos_y = new_pos_y;
}

void		Bonus5Part::draw(t_frame frame, IGraphicLibrary & graph) const
{
  if (frame >= m_timer)
    graph.draw(m_pos_x, m_pos_y, m_img);
}

bool		Bonus5Part::checkSnakePosition(t_frame frame, Snake const & snake) const
{
  if (frame >= m_timer && snake.getPosX() == m_pos_x && snake.getPosY() == m_pos_y)
    return (true);
  return (false);
}

void		Bonus5Part::move(t_frame frame, std::vector< std::vector<Case *> > const & map, Snake const & snake)
{
  if (frame >= m_timer && frame - m_lastmove >= m_framerate)
    {
      if (m_img == IGraphicLibrary::IMG_BONUS5PART_1)
	m_img = IGraphicLibrary::IMG_BONUS5PART_2;
      else
	m_img = IGraphicLibrary::IMG_BONUS5PART_1;
      m_lastmove = frame;
      if (frame - m_timer >= m_timeToCatch)
	generatePosition(frame, map, snake);
    }
}

t_frame		Bonus5Part::getLastMove(void) const
{
  return (m_lastmove);
}

t_frame		Bonus5Part::getFrameRate(void) const
{
  return (m_framerate);
}
